package com.controller;


import com.domain.Menu;
import com.domain.ResponseResult;
import com.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;
    /*
     * 查询所有父子菜单信息
     * */
    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu(){
        List<Menu> allMenu = menuService.findAllMenu();
        return new ResponseResult(true,200,"查询成功",allMenu);
    }
    /*
     * 根据id查找菜单
     * */
    @RequestMapping("/findMenuInfoById")
    public ResponseResult findMenuInfoById(Integer id){
        if (id == -1){
            //添加操作
            List<Menu> menus = menuService.findAllMenuByPid(-1);

            Map<String, Object> map = new HashMap<>();
            map.put("menuInfo",null);
            map.put("parentMenuList",menus);
            return new ResponseResult(true,200,"添加回显成功",map);
        }else {
            Menu menu = menuService.findMenuById(id);
            List<Menu> menus = menuService.findAllMenuByPid(-1);
            Map<String, Object> map = new HashMap<>();
            map.put("menuInfo",menu);
            map.put("parentMenuList",menus);
            return new ResponseResult(true,200,"修改回显成功",map);
        }
    }
    /*
     * 添加及修改菜单
     * */
    @RequestMapping("/saveOrUpdateMenu")
    public ResponseResult saveOrUpdateMenu(@RequestBody Menu menu){
        if (menu.getId() == null){
            menuService.saveMenu(menu);
            return new ResponseResult(true,200,"添加菜单成功",null);
        }else {
            menuService.updateMenu(menu);
            return new ResponseResult(true,200,"更改菜单成功",null);
        }
    }

}
